

import UIKit

import WebKit

class TFdocumentPreviewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
    
    @objc public var courseTitle:UILabel!
    var scrollView:UIScrollView =  UIScrollView.init()
    var webView = WKWebView.init()

    let bgview = UIView.init()
    let backBtn = TalkfunIncreaseButton.init()
    
    @objc public var urlStr:String!
    @objc public var localPath:String!

    //固定方向
    @objc public var fixedDirection = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       
            //TODO:监听手机方向
            NotificationCenter.default.addObserver(self, selector: #selector(self.deviceOrientationChange), name: Notification.Name(UIDevice.orientationDidChangeNotification.rawValue), object: nil)
       
        
        
     
        bgview.backgroundColor = UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 1.0)
        self.view.addSubview(bgview)
    
        
        backBtn.setImage(UIImage.init(named: "HTLive.bundle/docBack"), for: .normal)
        backBtn.addTarget(self, action: #selector(backBtnClick), for: .touchUpInside)
        self.view.addSubview(backBtn)
        
        self.courseTitle = UILabel.init()
        self.courseTitle.font = UIFont.PingFang(size: 18)
        self.courseTitle.text = "课件详情"
        self.courseTitle.textColor = .white
        self.courseTitle.textAlignment = .center
        bgview.addSubview(self.courseTitle)
        
        
        self.view.addSubview(self.scrollView)
        self.scrollView.layer.cornerRadius = 4
        self.scrollView.layer.masksToBounds = true
        self.view.backgroundColor = UIColor.white
      
        self.scrollView.addSubview(self.webView)
        self.webView.uiDelegate = self
        self.webView.navigationDelegate = self
        self.webView.scrollView.isScrollEnabled = true
        self.webView.isUserInteractionEnabled = true

        self.webView.backgroundColor = UIColor.clear
        if #available(iOS 11.0, *) {
            self.scrollView.contentInsetAdjustmentBehavior = .never
            
        }
        
        self.deviceOrientationChangeUI()
        
        
        let localPath:NSString = self.localPath as NSString
        
        if localPath.length == 0 {
            //加载在线的

            let url = NSURL.init(string: self.urlStr)
            let request = URLRequest(url: url! as URL)
            self.webView.load(request)
        } else {
            //加载离线的
        let file = FileManager.default
        let isEXit = file.fileExists(atPath:localPath as String)
            
        if isEXit {
            let url = NSURL.init(fileURLWithPath:localPath as String)

            if #available(iOS 9.0, *) {
                self.webView.loadFileURL(url as URL, allowingReadAccessTo: url as URL)
            } else {
                // Fallback on earlier versions
            }

          }
        }
    

    }
    @objc func deviceOrientationChange(){
        if self.fixedDirection == true {
            return
        }
        self.deviceOrientationChangeUI()
    }
    //自动换方向
    @objc func deviceOrientationChangeUI()
    {
        
        var SafetyZone:CGFloat = 0
        //刘海屏幕
        if (self.isNotchScreen) {
            SafetyZone = 32
        }
        
        
        bgview.frame = CGRect(x: 0, y: 0, width: TalkfunSharedTools.getMainScreen().size.width, height: 64 + SafetyZone)
   
        
        backBtn.frame = CGRect(x: 3, y: 22 + SafetyZone, width: 40, height: 40)
        
        self.courseTitle.frame = CGRect(x: backBtn.frame.maxX+3, y: 29.5 + SafetyZone, width: self.view.frame.size.width-86, height: 25)
        
        self.scrollView.frame = CGRect(x: 0, y: bgview.frame.maxY, width: self.view.frame.size.width, height: self.view.frame.size.height-bgview.frame.maxY)
        
        self.webView.frame = self.scrollView.bounds
        
        
        self.webView.reload()
        
    }
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        if self.fixedDirection {
            return [UIInterfaceOrientationMask.landscapeRight]
        }else
        {
            
            return  [UIInterfaceOrientationMask.landscapeRight, UIInterfaceOrientationMask.portrait]
            
        }
    }
   @objc func backBtnClick() {
  
       NotificationCenter.default.removeObserver(self)
           
       
       if ((self.navigationController) != nil) {
           self.navigationController?.popViewController(animated: true)
           
       }else{
           self.dismiss(animated: true)
          
       }
    }
    

}
